var authObj = {
    users : {},
    roles : {},
    resources : {},
	init : function() {
        authObj.users = eval($('#usersJson').val());
        authObj.roles = eval($('#rolesJson').val());
        authObj.resources = eval($('#resourcesJson').val());
		$("input[name=ozingUserIds]").change(authObj.selectUserChange);
		$("input[name=ozingRoleIds]").change(authObj.selectRoleChange);
		$("#operation").change(authObj.operationChange);

        $('#saveUseRoleBtn').click(authObj.submit);
        $('#saveRoleResourceBtn').click(authObj.submit);
        $('#createUserBtn').click(authObj.createUser);
        $('#deleteUserBtn').click(authObj.deleteUser);
        $('#createRoleBtn').click(authObj.createRole);
        $('#deleteRoleBtn').click(authObj.deleteRole);
	},
    selectUserChange : function() {
        $("input[name=ozingRoleIds]").removeAttr('checked');
        $("input[name=ozingResourceIds]").removeAttr('checked');
        var userId = $(this).val();
        $(authObj.users).each(function(i){
            var currentUser = $(this)[0];
            if (currentUser.id == userId) {
                $(currentUser.roles).each(function(i) {
                    var currentRole = $(this)[0];
                    $('#role_' + currentRole.id).attr('checked', 'checked');
                    $(currentRole.resources).each(function(i) {
                        $('#resource_' + $(this)[0].id).attr('checked', 'checked');
                    });
                });
                return false;
            }
        });
    },
    selectRoleChange : function() {
        $("input[name=ozingResourceIds]").removeAttr('checked');
        $("input[name=ozingRoleIds]:checked").each(function(i) {
            var roleId = $(this).val();
            $(authObj.roles).each(function(i){
                var currentRole = $(this)[0];
                if (currentRole.id == roleId) {
                    $(currentRole.resources).each(function(i) {
                        $('#resource_' + $(this)[0].id).attr('checked', 'checked');
                    });
                }
            });
        });
    },
    operationChange : function() {
        var operation = $('#operation').val();
        if (operation == 'user') {
            $("input[name=ozingUserIds]").removeAttr('disabled');
            $("input[name=ozingRoleIds][type=checkbox]").show();
            $("input[name=ozingRoleIds][type=radio]").hide();
            $("input[name=ozingResourceIds]").attr('disabled', 'disabled');

            $('#createUserBtn').show();
            $('#deleteUserBtn').show();
            $('#saveUseRoleBtn').show();
            $('#createRoleBtn').hide();
            $('#deleteRoleBtn').hide();
            $('#saveRoleResourceBtn').hide();
        } else if (operation == 'role') {
            $("input[name=ozingUserIds]").removeAttr('checked');
            $("input[name=ozingUserIds]").attr('disabled', 'disabled');
            $("input[name=ozingRoleIds][type=checkbox]").hide();
            $("input[name=ozingRoleIds][type=radio]").show();
            $("input[name=ozingResourceIds]").removeAttr('disabled');

            $('#createUserBtn').hide();
            $('#deleteUserBtn').hide();
            $('#saveUseRoleBtn').hide();
            $('#createRoleBtn').show();
            $('#deleteRoleBtn').show();
            $('#saveRoleResourceBtn').show();
        }
    },
    submit : function() {
        $.post("/ozing/auth",
            $("#authForm").serialize(),
            function(obj, statusText) {
                if (obj.result) {
                    alert(' 保存成功 ');
                    location.href = location.href;
                } else {
                    alert(obj.message);
                }
            }, 'json'
        );
    },
    createUser : function() {
        var username = window.prompt("请输入用户名,请确认该用户存在于UC中!","");
        if (username) {
            $.post("/ozing/auth",
                {username: username},
                function(obj, statusText) {
                    if (obj.result == true) {
                        alert(' 保存成功 ');
                        location.href = location.href;
                    } else {
                        alert(obj.message);
                    }
                }, 'json'
            );
        }
    },
    createRole : function() {
        var roleName = window.prompt("请输入角色名!","");
        if (roleName) {
            $.post("/ozing/auth",
                {roleName: roleName},
                function(obj, statusText) {
                    if (obj.result) {
                        alert(' 保存成功 ');
                        location.href = location.href;
                    } else {
                        alert(obj.message);
                    }
                }, 'json'
            );
        }
    },
    deleteUser : function() {
        var confirm = window.confirm("确认删除用户?");
        if (confirm) {
            var user = $("input[name=ozingUserIds]:checked");
            var userId = $(user).val();
            $.ajax({
                type : "POST",
                url : "/ozing/auth",
                data: {'_method': 'delete', 'userIds' :  userId},
                success : function(obj, statusText) {
                    if (obj.result) {
                        alert(' 删除成功 ');
                        location.href = location.href;
                    } else {
                        alert(obj.message);
                    }
                } ,
                dataType : "json"
            });
        }
    },
    deleteRole : function() {
        var confirm = window.confirm("确认删除角色?");
        if (confirm) {
            var role = $("input[name=ozingRoleIds]:checked");
            var roleId = $(role).val();
            $.ajax({
                type : "POST",
                url : "/ozing/auth",
                data: {'_method': 'delete', 'roleIds' :  roleId},
                success : function(obj, statusText) {
                    if (obj.result) {
                        alert(' 删除成功 ');
                        location.href = location.href;
                    } else {
                        alert(obj.message);
                    }
                }
            });
        }
    }
};

$(document).ready(authObj.init);